package org.farng.mp3.object;

/**
 * Title: Description: Copyright:    Copyright (c) 2002 Company:
 *
 * @author Eric Farng
 * @version $Revision: 1.8 $
 */
public class ObjectLyrics3TimeStamp extends AbstractMP3Object {
    /** DOCUMENT ME! */
    private long minute = 0;

    /** DOCUMENT ME! */
    private long second = 0;

    /**
     * Creates a new ObjectLyrics3TimeStamp object.
     *
     * @param identifier DOCUMENT ME!
     */
    public ObjectLyrics3TimeStamp(String identifier) {
        this.identifier = identifier;
    }

    public ObjectLyrics3TimeStamp(ObjectLyrics3TimeStamp copyObject) {
        super(copyObject);
        this.minute = copyObject.minute;
        this.second = copyObject.second;
    }

    public void setMinute(long minute) {
        this.minute = minute;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public long getMinute() {
        return minute;
    }

    public void setSecond(long second) {
        this.second = second;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public long getSecond() {
        return second;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        return 7;
    }

    /**
     * Creates a new ObjectLyrics3TimeStamp object.
     *
     * @param timeStamp DOCUMENT ME!
     * @param timeStampFormat DOCUMENT ME!
     */
    public void setTimeStamp(long timeStamp, byte timeStampFormat) {
        /**
         * @todo convert both types of formats
         */
        timeStamp = timeStamp / 1000;
        minute    = timeStamp / 60;
        second    = timeStamp % 60;
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectLyrics3TimeStamp) == false) {
            return false;
        }

        ObjectLyrics3TimeStamp object = (ObjectLyrics3TimeStamp) obj;

        if (this.minute != object.minute) {
            return false;
        }

        if (this.second != object.second) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param timeStamp DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readString(String timeStamp, int offset) {
        if (timeStamp == null) {
            throw new NullPointerException("Image is null");
        }

        if ((offset < 0) || (offset >= timeStamp.length())) {
            throw new IndexOutOfBoundsException("Offset to timeStamp is out of bounds: offset = " + offset + ", timeStamp.length()" + timeStamp.length());
        }

        timeStamp = timeStamp.substring(offset);

        if (timeStamp.length() == 7) {
            minute = Integer.parseInt(timeStamp.substring(1, 3));
            second = Integer.parseInt(timeStamp.substring(4, 6));
        } else {
            minute = 0;
            second = 0;
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        return writeString();
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String writeString() {
        String str;
        str = "[";

        if (minute < 0) {
            str += "00";
        } else {
            if (minute < 10) {
                str += '0';
            }

            str += Long.toString(minute);
        }

        str += ':';

        if (second < 0) {
            str += "00";
        } else {
            if (second < 10) {
                str += '0';
            }

            str += Long.toString(second);
        }

        str += ']';

        return str;
    }
}